#include "Character.h"
using namespace cocos2d;
using namespace cocos2d::extension;
using namespace std;

Character::Character(void)
{
}


Character::~Character(void)
{
}

cocos2d::extension::CCSkeleton* Character::LoadSpineRes()
{
	mSkeletonNode = CCSkeleton::createWithFile("spine/spineboy.json", "spine/spineboy.atlas");
	AnimationStateData_setMixByName(mSkeletonNode->state->data, "walk", "jump", 0.4f);
	AnimationStateData_setMixByName(mSkeletonNode->state->data, "jump", "walk", 0.4f);
	AnimationState_setAnimationByName(mSkeletonNode->state, "walk", true);
	mSkeletonNode->timeScale = 0.3f;
	mSkeletonNode->debugBones = true;

	mSkeletonNode->runAction(CCRepeatForever::create(CCSequence::create(CCFadeOut::create(1),
		CCFadeIn::create(1),
		CCDelayTime::create(5),
		NULL)));

	CCSize windowSize = CCDirector::sharedDirector()->getWinSize();
	mSkeletonNode->setPosition(ccp(windowSize.width / 2, 20));	

	mStatus = StatusGround;

	return mSkeletonNode;
}

void Character::PlayAction(int key)
{
	switch(key)
	{
	case KEY_UP:
		AnimationState_setAnimationByName(mSkeletonNode->state, "jump", false);
		break;
	case KEY_DOWN:
		if(mStatus == StatusGround)
			AnimationState_setAnimationByName(mSkeletonNode->state, "sitdown", false);
		else
			AnimationState_setAnimationByName(mSkeletonNode->state, "down", false);
		break;
	case KEY_LEFT:
		AnimationState_setAnimationByName(mSkeletonNode->state, "left", false);
		break;
	case KEY_RIGHT:
		AnimationState_setAnimationByName(mSkeletonNode->state, "right", false);
		break;
	default:
		break;
	}
}

void Character::Update( float dt )
{
	if (mSkeletonNode->state->loop) {
		//if (skeletonNode->state->time > 2) AnimationState_setAnimationByName(skeletonNode->state, "jump", false);
	} else {
		if (mSkeletonNode->state->time > 1) AnimationState_setAnimationByName(mSkeletonNode->state, "walk", true);
	}
}